package dataRetriever;

import java.sql.SQLException;
import java.util.ArrayList;

import logger.GRIALogger;

import dBConnection.dBConnector;

import universityMap.Agreement;

/**
 * This class generates and selects the data in order to create the KML
 * document.
 * 
 * @author Andrea Piermarteri, Matteo Micheletti, Diego Cervellini.
 * 
 */
public class DataCreator {

	/**
	 * This method generates the ArrayList of all the Agreements from the GRIA's
	 * database.
	 * 
	 * @param url
	 * @param dbName
	 * @param user
	 * @param pwd
	 * @return the ArrayList of all the Agreements.
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public static ArrayList<Agreement> createDataFromDBConnector(String url,
			String dbName, String user, String pwd) throws SQLException,
			ClassNotFoundException {

		dBConnector connector = new dBConnector(url, dbName, user, pwd);

		ArrayList<Agreement> result = connector.getAgreements();

		connector.closeConnection();

		return result;
	}

	/**
	 * Given an ArrayList of Agreements and the name of an Institution, this
	 * method returns all the Agreement with that institution as first
	 * institution.
	 * 
	 * @param input
	 * @param name
	 * @return
	 */
	public static ArrayList<Agreement> selectI1(ArrayList<Agreement> input,
			String name) {
		ArrayList<Agreement> result = new ArrayList<Agreement>();
		for (Agreement a : input) {
			if (a.getI1().name.trim().equalsIgnoreCase(name)) {
				result.add(a);
			}
		}
		return result;
	}

	/**
	 * Given an ArrayList of Agreements and the name of an Institution, this
	 * method returns all the Agreement with that institution as second
	 * institution.
	 * 
	 * @param input
	 * @param name
	 * @return
	 */
	public static ArrayList<Agreement> selectI2(ArrayList<Agreement> input,
			String name) {
		ArrayList<Agreement> result = new ArrayList<Agreement>();
		for (Agreement a : input) {
			if (a.getI2().name.trim().equalsIgnoreCase(name)) {
				result.add(a);
			}
		}
		return result;
	}

	/**
	 * Given an ArrayList of Agreements and a type of Agreement, this method
	 * returns all the Agreement of that type.
	 * 
	 * @param input
	 * @param type
	 * @return
	 */
	public static ArrayList<Agreement> selectAgreement(
			ArrayList<Agreement> input, String type) {
		ArrayList<Agreement> result = new ArrayList<Agreement>();
		for (Agreement a : input) {
			if (a.type.trim().equalsIgnoreCase(type)) {
				result.add(a);
			}
		}
		return result;
	}

	/**
	 * Given an ArrayList of Agreements and a country, this method returns all
	 * the Agreement where the second Institution belong to that country.
	 * 
	 * @param input
	 * @param country
	 * @return
	 */
	public static ArrayList<Agreement> selectCountry(
			ArrayList<Agreement> input, String country) {
		ArrayList<Agreement> result = new ArrayList<Agreement>();
		for (Agreement a : input) {
			if (a.getI2().country.trim().equalsIgnoreCase(country)) {
				result.add(a);
			}
		}
		return result;
	}

	/**
	 * This method is a composition of all the selects before (you can avoid to
	 * do one of the selection setting the corresponding parameter to "").
	 * 
	 * @param input
	 * @param name1
	 * @param name2
	 * @param agrType
	 * @param country
	 * @return
	 */
	public static ArrayList<Agreement> select(ArrayList<Agreement> input,
			String name1, String name2, String agrType, String country) {
		ArrayList<Agreement> result = input;

		if (!name1.trim().equalsIgnoreCase("")) {
			result = DataCreator.selectI1(result, name1);
		}

		if (!name2.trim().equalsIgnoreCase("")) {
			result = DataCreator.selectI2(result, name2);
		}

		if (!agrType.trim().equalsIgnoreCase("")) {
			result = DataCreator.selectAgreement(result, agrType);
		}

		if (!country.trim().equalsIgnoreCase("")) {
			result = DataCreator.selectCountry(result, country);
		}
		return result;
	}
}
